package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.entity.Category;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryServiceImpl;
    @PostMapping
    public R<String> addCategory(HttpSession session, @RequestBody Category category){
        Long employee = (Long) session.getAttribute("employee");
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateUser(employee);
        category.setCreateUser(employee);
        categoryServiceImpl.save(category);
        return R.success("添加成功");
    }
    @GetMapping("/page")
    public R<Page> pageQueryCategory(@RequestParam("page")Integer page,
                                      @RequestParam("pageSize") Integer pageSize){
        Page<Category> categoryPage = new Page<>(page,pageSize);
        categoryServiceImpl.page(categoryPage);
        return R.success(categoryPage);
    }
}
